package com.jingtai.platform.service.shop.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jingtai.common.enums.DeleteFlagEnum;
import com.jingtai.common.enums.EnableStatusEnum;
import com.jingtai.common.enums.InputEnum;
import com.jingtai.common.enums.LevelEnum;
import com.jingtai.common.template.ResultMapInfo;
import com.jingtai.common.utils.random.TokenUtil;
import com.jingtai.core.entity.shop.*;
import com.jingtai.core.entity.supply.SupplyGoods;
import com.jingtai.core.mapper.shop.GoodsSpecificationsCustomMapper;
import com.jingtai.core.mapper.shop.GoodsSpecificationsMapper;
import com.jingtai.platform.service.shop.*;
import com.jingtai.platform.service.supply.SupplyGoodsService;
import com.jingtai.platform.util.ResultUtil;
import com.jingtai.platform.util.UrlToMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 商品规格表 impl
 *
 * @author sunlaiqian
 * @date 2020-12-09 15:39:05
 */
@Service
public class GoodsSpecificationsCustomServiceImpl extends ServiceImpl<GoodsSpecificationsCustomMapper, GoodsSpecificationsCustom> implements GoodsSpecificationsCustomService {

    @Autowired
    private GoodsSpecificationsCustomService goodsSpecificationsCustomService;

    @Override
    public ResultUtil GoodsSpecificationsCustomlist(Integer page, Integer limit, String conditions){
        QueryWrapper<GoodsSpecificationsCustom> wrapper = new QueryWrapper<>();
        Map<String, String> map = UrlToMap.paramToMap(conditions);
        for(Map.Entry<String, String> entry : map.entrySet()){
            wrapper.like(entry.getKey(), entry.getValue());
        }
        wrapper.eq("delete_flag", DeleteFlagEnum.NOT_DELETE.getCode());
        return new ResultUtil(ResultMapInfo.GETSUCCESS,goodsSpecificationsCustomService.page(new Page<>(page,limit),wrapper));
    }

    @Override
    public ResultUtil addGoodsSpecificationsCustom(GoodsSpecificationsCustom goodsSpecificationsCustom) {
        boolean save = goodsSpecificationsCustomService.save(goodsSpecificationsCustom);
        if (save){
            return new ResultUtil(ResultMapInfo.ADDSUCCESS,goodsSpecificationsCustom.getId());
        }
        return new ResultUtil(ResultMapInfo.ADDFAIL);
    }

    @Override
    public ResultUtil getGoodsSpecificationsCustomById(Long id) {
        GoodsSpecificationsCustom goodsSpecificationsCustom = goodsSpecificationsCustomService.getById(id);
        return goodsSpecificationsCustom == null ?  new ResultUtil(ResultMapInfo.GETFAIL) : new ResultUtil(ResultMapInfo.GETSUCCESS,goodsSpecificationsCustom);
    }

    @Override
    public ResultUtil delGoodsSpecificationsCustomById(Long id) {
        GoodsSpecificationsCustom goodsSpecificationsCustom = new GoodsSpecificationsCustom();
        goodsSpecificationsCustom.setId(id);
        goodsSpecificationsCustom.setDeleteFlag(DeleteFlagEnum.DELETE.getCode());
        boolean delState = goodsSpecificationsCustomService.updateById(goodsSpecificationsCustom);
        return delState ?  new ResultUtil(ResultMapInfo.DELETESUCCESS) : new ResultUtil(ResultMapInfo.DELETEFAIL);
    }

    @Override
    public ResultUtil selectGoodsSpecificationsCustomOfOne(String goodsCode) {
        List<GoodsSpecificationsCustom> list = goodsSpecificationsCustomService.list(Wrappers.lambdaQuery(GoodsSpecificationsCustom.class).eq(GoodsSpecificationsCustom::getGoodsCode,goodsCode).eq(GoodsSpecificationsCustom::getLevel, LevelEnum.ONELEVEL.getValue()).eq(GoodsSpecificationsCustom::getEnableState, EnableStatusEnum.NORMAL.getValue()).eq(GoodsSpecificationsCustom::getDeleteFlag, DeleteFlagEnum.NOT_DELETE.getCode()));
        return new ResultUtil(ResultMapInfo.GETSUCCESS,list);
    }

    @Override
    public ResultUtil selectGoodsSpecificationsCustomOfAll() {
        //先查询一级分类
        List<GoodsSpecificationsCustom> list = goodsSpecificationsCustomService.list(Wrappers.lambdaQuery(GoodsSpecificationsCustom.class).eq(GoodsSpecificationsCustom::getLevel, LevelEnum.ONELEVEL.getValue()).eq(GoodsSpecificationsCustom::getEnableState, EnableStatusEnum.NORMAL.getValue()).eq(GoodsSpecificationsCustom::getDeleteFlag, DeleteFlagEnum.NOT_DELETE.getCode()));
        //根据一级分类查询二级分类
        List<JSONObject> jsonObjectList = new ArrayList<>();
        for (GoodsSpecificationsCustom goodsSpecificationsCustom : list){
            JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(goodsSpecificationsCustom));
            List<GoodsSpecificationsCustom> list2 = goodsSpecificationsCustomService.list(Wrappers.lambdaQuery(GoodsSpecificationsCustom.class).eq(GoodsSpecificationsCustom::getLevelOfOne,goodsSpecificationsCustom.getId()).eq(GoodsSpecificationsCustom::getLevel, LevelEnum.TWOLEVEL.getValue()).eq(GoodsSpecificationsCustom::getEnableState, EnableStatusEnum.NORMAL.getValue()).eq(GoodsSpecificationsCustom::getDeleteFlag, DeleteFlagEnum.NOT_DELETE.getCode()));
            jsonObject.put("list",list2);
            jsonObjectList.add(jsonObject);
        }
        return new ResultUtil(ResultMapInfo.GETSUCCESS,jsonObjectList);
    }

    @Override
    public ResultUtil editGoodsSpecificationsCustom(GoodsSpecificationsCustom goodsSpecificationsCustom) {
        boolean editState = goodsSpecificationsCustomService.updateById(goodsSpecificationsCustom);
        return editState ?  new ResultUtil(ResultMapInfo.EDITSUCCESS) : new ResultUtil(ResultMapInfo.EDITFAIL);
    }

}
